package com.br.fsm.projectdelivery.repositorio;

import java.util.ArrayList;

import android.database.Cursor;
import android.util.Log;

import com.br.fsm.projectdelivery.activity.R;
import com.br.fsm.projectdelivery.basica.Promocao;
import com.br.fsm.projectdelivery.basica.Promocao.Promocoes;
import com.br.fsm.projectdelivery.excecao.RepositorioException;
import com.br.fsm.projectdelivery.util.Constantes;

public class RepositorioPromocoes extends RepositorioBasico implements IRepositorioPromocoes {

	private static RepositorioPromocoes instance;	
	
	public static final String TABLE_NAME = "PROMOCOES";
	
	public static RepositorioPromocoes getInstance(){
		if ( instance == null ){
			instance =  new RepositorioPromocoes();
		} 
		return instance;
	}

	@Override
	public Promocao getPromocoesById(int id) throws RepositorioException {
		Cursor cursor = null;
		
		Promocao promocoes = null;
		
		try {
			cursor = db.query(TABLE_NAME, Promocao.getcolumns(), Promocoes.ID + " = " + id , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				promocoes = new Promocao();
				
				int idxId = cursor.getColumnIndex(Promocoes.ID);
				double idxDuracao = cursor.getColumnIndex(Promocoes.DURACAO);
				int idxDicaSemana = cursor.getColumnIndex(Promocoes.DICASEMANA);
				double idxValUnitario = cursor.getColumnIndex(Promocoes.VALUNITARIO);
				
								
				promocoes.setPromocaoId(cursor.getInt(idxId));
				promocoes.setPromocaoDuracao(cursor.getDouble((int) idxDuracao));
				promocoes.setPromocaoDicaSemana(cursor.getInt(idxDicaSemana));
				promocoes.setPromocaoValUnitario(cursor.getDouble((int) idxValUnitario));
				}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e( Constantes.CATEGORIA , e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return promocoes;
	}

	@Override
	public ArrayList<Promocao> getAllPromocoes() throws RepositorioException {
		Cursor cursor = null;
		
		ArrayList<Promocao> promocoes = null;
		try {
			promocoes = new ArrayList<Promocao>();
			cursor = db.query(TABLE_NAME, Promocao.getcolumns(), null , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				int idxId = cursor.getColumnIndex(Promocoes.ID);
				double idxDuracao = cursor.getColumnIndex(Promocoes.DURACAO);
				int idxDicaSemana = cursor.getColumnIndex(Promocoes.DICASEMANA);
				double idxValUnitario = cursor.getColumnIndex(Promocoes.VALUNITARIO);
					
				do {
					Promocao promocao = new Promocao();
					
					promocao.setPromocaoId(cursor.getInt(idxId));
					promocao.setPromocaoDuracao(cursor.getDouble((int) idxDuracao));
					promocao.setPromocaoDicaSemana(cursor.getInt(idxDicaSemana));
					promocao.setPromocaoValUnitario(cursor.getDouble((int) idxValUnitario));
					
					promocoes.add(promocao);
				} while (cursor.moveToNext());
				
			}
		} catch (Exception e) {
			e.printStackTrace();			
			Log.e( Constantes.CATEGORIA, e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return promocoes;
	}

	@Override
	public void insertPromocoes(Promocoes promocoes) throws RepositorioException {
		/*ContentValues values = new ContentValues();

		values.put(Promocoes.ID, promocoes.getPromocoesId());
		values.put(Promocoes.DURACAO, promocoes.getPromocoessDuracao());
		values.put(Promocoes.DICASEMANA, promocoes.getPromocoesDicaSemana());
		values.put(Promocoes.VALUNITARIO, promocoes.getPromocoesValUnitario());
		
		try {
			db.insert(TABLE_NAME, "", values);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}*/		
	}

	@Override
	public void updatePromocoes(Promocoes promocoes) throws RepositorioException {
		/*ContentValues values = new ContentValues();

		values.put(Promocoes.ID, promocoes.getPromocoesId());
		values.put(Promocoes.DURACAO, promocoes.getPromocoesDuracao());
		values.put(Promocoes.DICASEMANA, promocoes.getPromocoesDicaSemana());
		values.put(Promocoes.VALUNITARIO, promocoes.getPromocoesValUnitario());
		

		String _id = String.valueOf(Promocoes.getPromocoesId());

		String where = Promocoes.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.update(TABLE_NAME, values, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}*/
	}

	@Override
	public void removePromocoes(Promocoes promocoes) throws RepositorioException {
		/*String _id = String.valueOf(promocoes.getPromocoesId());

		String where = Promocoes.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.delete( TABLE_NAME, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}*/
	}

}
